﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：ComponentContainer
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/26 15:35:59
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.Collections.Generic;
using TouchFish.Actor;
using UnityEngine;

namespace TouchFish.Component
{
    public class ComponentContainer : IComponentContainer
    {
        private Dictionary<string, IComponent> _componentDic;

        public string Name { get; set; }

        public IActor Actor { get; set; }

        public ComponentContainer(string name)
        {
            Name = name;
            Init();
        }

        public void Init()
        {
            _componentDic = new Dictionary<string, IComponent>();
        }

        public void Add(IComponent component)
        {
            _componentDic.TryAdd(component.Name, component);
            component.InitAdd();
        }

        public IComponent Get(string componentName)
        {
            if (IsComponentExist(componentName))
            {
                IComponent component = null;
                _componentDic.TryGetValue(componentName, out component);
                return component;
            }
            Debug.LogError("_componentDic is not Contain " + componentName);
            return null;
        }

        public bool IsComponentExist(string componentName)
        {
            return _componentDic.ContainsKey(componentName);
        }

        public void Remove(IComponent component)
        {
            Remove(component.Name);
        }

        public T Get<T>(string componentName) where T : class, IComponent
        {
            if (IsComponentExist(componentName))
            {
                IComponent component = null;

                _componentDic.TryGetValue(componentName, out component);
                return (T)component;
            }

            Debug.LogError("_componentDic is not Contain " + componentName);
            return null;
        }

        public void Remove(string componentName)
        {
            _componentDic.Remove(componentName);
        }

        public void Dispose()
        {
            foreach (var kv in _componentDic)
            {
                kv.Value.Dispose();
            }
            _componentDic.Clear();
            _componentDic = null;
        }

        public void UpdateExecute()
        {
        }

        public void FixedExecute()
        {
        }

        public void LateExecute()
        {
        }

        public virtual void InitAdd()
        {
        }
    }
}